package jeni2.VCD;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class buatdatabase {
	
	public static final String KEY_judul = "judul";
	public static final String KEY_ROWID = "_id";
	public static final String KEY_kategori = "kategori";
	public static final String KEY_hargasewa = "hargasewa";
	
	private static final String TAG = "LocalDatabase";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_CREATE = "create table vcd (_id integer primary key autoincrement , " + " judul text not null, kategori text not null, hargasewa text not null);";
	
	private static final String DATABASE_NAME = "rentalvcd";
	private static final String DATABASE_TABLE = "vcd";
	private static final int DATABASE_VERSION = 2;
	private final Context mCtx;
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL(DATABASE_CREATE);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
			db.execSQL("Drop table if exists friend");
			onCreate(db);
		}
	}
	
	public buatdatabase (Context ctx)
	{
		this.mCtx = ctx;
	}
	
	public buatdatabase open() throws SQLException
	{
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		mDbHelper.close();
	}
	
	public long createAddress(String judul, String kategori, String hargasewa)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_judul, judul);
		initialValues.put(KEY_kategori, kategori);
		initialValues.put(KEY_hargasewa, hargasewa);
				
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}
	
	public boolean deleteAddress (long rowId)
	{
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" +rowId, null) > 0;
	}
	
	public Cursor fetchAllAddress()
	{
		return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_judul,KEY_kategori,KEY_hargasewa}, null, null, null, null,null);
	}
	
	public Cursor fetchAddress(long rowId) throws SQLException
	{
		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_judul,KEY_kategori,KEY_hargasewa}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null)
		{
			mCursor.moveToFirst();
		}

		return mCursor;
	}
	
	public boolean updateAddress(long rowId, String judul,String kategori,String hargasewa)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_judul, judul);
		args.put(KEY_kategori, kategori);
		args.put(KEY_hargasewa, hargasewa);
		
		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
